<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Modules\Admin\Entities\Water\WaterDeviceModel;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PublicController extends BaseController
{
    public function junda(Request $request): JsonResponse
    {
        $params = $request->all();
        $water_id = $params['water_id'];
        $WaterDeviceInfo = WaterDeviceModel::getInstance()->getSelectListsByWaterId($water_id);

        $equipmentName = $WaterDeviceInfo['mineral_title'];
        $start_date = $params['start_date'];
        if (!$params['junda_uuid']){
            return $this->errorJson('请输入骏达uuid');
        }
        if (!$params['start_date']){
            return $this->errorJson('请选择日期');
        }
        $uuid = $params['junda_uuid']??"fb2bc4a5-2598-4525-a380-b271011fceca";
        $date = Carbon::createFromFormat('Y-m-d', $start_date);
        $date->subDay(); // 将日期增加一天
        $StartDate = $date->copy()->setTime(16, 0, 0)->format('Y-m-d\TH:i:s.v\Z');
        $post = [
            'current'=>1,
            'size'=> 10,
            'EquipmentName'=> $equipmentName,
            'EndDate'=> $StartDate,
            'StartDate'=> $StartDate,
            'CardNo'=> '',
            'conType'=> '',
        ];

        $result =$this->curl_request_junda($post,$uuid);
        $ConMoney = $result['ConMoney']??0;
        $rows = $result['rows']??[];

//        if ($ConMoney>110){
//            $ConMoney = rand(50,100);
//        }


        return $this->successJson(['score'=>$ConMoney,'rows'=>$rows],'错误');
    }
    public function curl_request_junda($post,$uuid){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://webapi.ljda.net/BusDataCenter/GetConsumeDetailList');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Accept: application/json, text/plain, */*',
            'Accept-Language: zh-CN,zh;q=0.9',
            'Connection: keep-alive',
            'Content-Type: application/json;charset=UTF-8',
            'Origin: http://oa.ljda.net',
            'Referer: http://oa.ljda.net/',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
            'X-Token: '.$uuid.'',
            'uuid: '.$uuid.'',
        ]);
//        curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"current\":1,\"size\":10,\"EquipmentName\":\"鲍山花园北区小广场\",\"EndDate\":\"2024-12-31T16:00:00.000Z\",\"StartDate\":\"2024-12-31T16:00:00.000Z\",\"CardNo\":\"\",\"conType\":\"\"}");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        curl_close($ch);
        $object = json_decode($response);
        $return = object_to_array($object);

        return $return;
    }

}
